<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020/5/9
 * Time: 14:53
 */

namespace App\Rpc\Traits\Service;

use App\Rpc\Model\Dao\OrganizationDao;
use Swoft\Bean\BeanFactory;
use App\Lib\Error\Response;
use App\Rpc\Model\Logic\PermissionLogic;
use App\Lib\Error\ErrorConstant;

/**
 * Class OrganizationTrait
 * @package App\Rpc\Traits
 */
trait OrganizationTrait
{
    /**
     * @param array $data
     * @return array
     */
    public function organizationCreate(array $data): array
    {
        //验证数据是否合理
        $diyValidator = [
            'OrganizationValidator' => [ 'op' => 'add']
        ];
        try{
            //添加item_id
            $data['item_id'] = create_item_id($data['uuid'])??null;
            //这里首先验证中间件
            validate($data, "OrganizationValidator", [], $diyValidator);
            /** @var  OrganizationDao $Dao */
            $Dao = BeanFactory::getBean(OrganizationDao::class);
            $result = $Dao->insert($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function organizationUpdate(array $data): array
    {
        //验证数据是否合理更新
        $diyValidator = [
            'OrganizationValidator' => [ 'op' => 'update']
        ];
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            //这里首先验证中间件
            validate($data, "OrganizationValidator", [], $diyValidator);
            /** @var OrganizationDao $Dao */
            $Dao = BeanFactory::getBean(OrganizationDao::class);
            $result = $Dao->update($id, $data);
            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function organizationDelete(array $data): array
    {
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);
            /** @var OrganizationDao $Dao */
            $Dao = BeanFactory::getBean(OrganizationDao::class);
            $result = $Dao->delete($id);

            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 组织授权用户
     * @param array $data
     * @return array
     */
    public function organizationModifyUsers(array $data): array
    {
        //根绝item_id添加用户人员
        try{
            $organization_item_id = $data['organization_item_id']??null;

            $attached_account_item_ids = $data['attached_account_item_ids']??[];

            $detached_account_item_ids = $data['detached_account_item_ids']??[];

            if(!$organization_item_id){
                return Response::FatalError(ErrorConstant::REQUEST_PARAMS_ERROR_CODE, ErrorConstant::REQUEST_PARAMS_ERROR_TITLE);
            }

            if(!$attached_account_item_ids && !$detached_account_item_ids){
                return Response::FatalError(ErrorConstant::REQUEST_PARAMS_ERROR_CODE, ErrorConstant::REQUEST_PARAMS_ERROR_TITLE);
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */

            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->organizationModifyUsers($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }

        //参数：$data['organization_item_id']
        //参数：$data['user_item_ids'] = []
        //更改某个角色的用户
    }

    /**
     * @param array $data
     * @return array
     */
    public function userOrganizations(array $data): array
    {
        try{
            //参数验证
            $user_item_id = $data['user_item_id']??null;
            if(!$user_item_id){
                return Response::FatalError(4002, '请求参数错误');
            }

            //接下来查询所有得role_permissions
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->userOrganizations($user_item_id);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }

        //参数：$data['user_item_id']
        //首先：获取该用户的组织
        //最后：获取到该用户计入的组织
    }
}
